package com.yipei.business.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yipei.business.domain.MlImportantNews;
import com.yipei.business.service.IMlImportantNewsService;
import com.yipei.common.config.RuoYiConfig;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.utils.DateUtils;
import com.yipei.common.utils.StringUtils;
import com.yipei.common.utils.file.FileUploadUtils;
import com.yipei.common.utils.file.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlLastHotNewsMapper;
import com.yipei.business.domain.MlLastHotNews;
import com.yipei.business.service.IMlLastHotNewsService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 必读新闻Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlLastHotNewsServiceImpl implements IMlLastHotNewsService
{
    @Autowired
    private MlLastHotNewsMapper mlLastHotNewsMapper;

    @Autowired
    private IMlImportantNewsService iMlImportantNewsService;

    /**
     * 查询必读新闻
     *
     * @param id 必读新闻主键
     * @return 必读新闻
     */
    @Override
    public MlLastHotNews selectMlLastHotNewsById(Long id)
    {
        return mlLastHotNewsMapper.selectMlLastHotNewsById(id);
    }

    /**
     * 查询必读新闻列表
     *
     * @param mlLastHotNews 必读新闻
     * @return 必读新闻
     */
    @Override
    public List<MlLastHotNews> selectMlLastHotNewsList(MlLastHotNews mlLastHotNews)
    {
        return mlLastHotNewsMapper.selectMlLastHotNewsList(mlLastHotNews);
    }

    /**
     * 新增必读新闻
     *
     * @param mlLastHotNews 必读新闻
     * @return 结果
     */
    @Override
    public int insertMlLastHotNews(MlLastHotNews mlLastHotNews)
    {
        mlLastHotNews.setCreateTime(DateUtils.getNowDate());
        return mlLastHotNewsMapper.insertMlLastHotNews(mlLastHotNews);
    }

    /**
     * 修改必读新闻
     *
     * @param mlLastHotNews 必读新闻
     * @return 结果
     */
    @Override
    public int updateMlLastHotNews(MlLastHotNews mlLastHotNews)
    {
        mlLastHotNews.setUpdateTime(DateUtils.getNowDate());
        return mlLastHotNewsMapper.updateMlLastHotNews(mlLastHotNews);
    }

    /**
     * 批量删除必读新闻
     *
     * @param ids 需要删除的必读新闻主键
     * @return 结果
     */
    @Override
    public int deleteMlLastHotNewsByIds(Long[] ids)
    {
        return mlLastHotNewsMapper.deleteMlLastHotNewsByIds(ids);
    }

    /**
     * 删除必读新闻信息
     *
     * @param id 必读新闻主键
     * @return 结果
     */
    @Override
    public int deleteMlLastHotNewsById(Long id)
    {
        return mlLastHotNewsMapper.deleteMlLastHotNewsById(id);
    }

    @Override
    public void iMlLastHotNewsService() throws Exception {
        HttpClient client = HttpClients.createDefault();
        HttpGet request = new HttpGet("https://np-listapi.eastmoney.com/comm/web/getNewsByColumns?client=web&biz=web_news_col&column=345&order=1&needInteractData=0&page_index=1&page_size=20&req_trace=1735615491220"); //示例URL，请替换为实际的股票代码
        request.setHeader("Content-Type", "text/javascript;charset=UTF-8");
        HttpResponse response = client.execute(request);
        List<MlLastHotNews> list= new ArrayList<>();
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            JSONObject jsonObject =  JSONObject.parseObject(result).getJSONObject("data");
            JSONArray list1 = jsonObject.getJSONArray("list");
            list = list1.toJavaList(MlLastHotNews.class).subList(0,20);
            for (MlLastHotNews mlLastHotNews : list) {
                mlLastHotNews.setContent(mlLastHotNews.getSummary());
                mlLastHotNews.setFbSj(mlLastHotNews.getShowTime());
                mlLastHotNews.setXwLy(mlLastHotNews.getMediaName());
                String image = mlLastHotNews.getImage();
                if (StringUtils.isNotEmpty(image)){
                    MultipartFile multipartFile = iMlImportantNewsService.downloadFileAsMultipartFile(image, mlLastHotNews.getMediaName());
                    AjaxResult ajaxResult = iMlImportantNewsService.uploadFile(multipartFile);
                    mlLastHotNews.setImg(String.valueOf(ajaxResult.get("fileName")));
                }
            }
        }
        for (MlLastHotNews mlImportantNews: list) {
            this.insertMlLastHotNews(mlImportantNews);
        }
    }
}
